//
//  CreditView.m
//  phmall
//
//  Created by admin on 2017/12/18.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "CreditView.h"
#define Button_Tag 88
@implementation CreditView

-(instancetype)init{
    if (self=[super init]) {
        
        self.headView=[[UIView alloc]init];
        [self addSubview:self.headView];
        self.headView.backgroundColor=[UIColor orangeColor];
        [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(0);
            make.left.right.equalTo(self);
       
        }];
        
        self.items = @[Localized(@"最近一周"),Localized(@"最近三月"),Localized(@"最近六月"),Localized(@"汇总")];
        CGFloat itemWidth = SCREEN_WIDTH/self.items.count;
        for (int i = 0; i< self.items.count;i++ ) {
            UIButton * buton = [UIButton buttonWithType:UIButtonTypeCustom];
            [self.headView addSubview:buton];
            [buton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self).offset(0);
                make.left.equalTo(self).with.offset(itemWidth*i);
                make.height.offset(44);
                make.width.offset(itemWidth);
            }];
            [buton setTitle:self.items[i] forState:UIControlStateNormal];
            [buton setTitleColor:[UIColor colorWithHexString:@"#FFAC50"]forState:UIControlStateNormal];
            [buton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
            buton.titleLabel.font=[UIFont systemFontOfSize:13];
            [buton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            buton.tag=Button_Tag+i;
            if (i == 0) {
                buton.selected = YES;
            }
        }
        self.line=[UIView new];
        self.line.backgroundColor=[UIColor colorWithHexString:@"#FFAC50"];
        [self.headView addSubview:self.line];
        [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(44);
            make.left.offset(20);
            make.width.offset(SCREEN_WIDTH-40);
            make.height.mas_equalTo(1);
        }];
        
        for (int i=0; i<3; i++) {
            
        
            UILabel *evaluate=[[UILabel alloc]init];
            [self.headView addSubview:evaluate];
           
            evaluate.textAlignment=NSTextAlignmentCenter;
            evaluate.textColor=[UIColor whiteColor];
            evaluate.font=[UIFont systemFontOfSize:13];
            [evaluate mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.line.mas_bottom).offset(0);
                make.left.offset(i*(SCREEN_WIDTH/3));
                make.height.offset(44);
                make.width.offset(SCREEN_WIDTH/3);
            }];
            
            switch (i) {
                case 0:
                    evaluate.text=Localized(@"好评");
                    break;
                case 1:
                    evaluate.text=Localized(@"中评");
                    break;
                case 2:
                    evaluate.text=Localized(@"差评");
                    break;
                default:
                    break;
            }
            
            UIView *line=[[UIView alloc]init];
            line.backgroundColor=[UIColor whiteColor];\
            line.tag=2048+i;
            [self.headView addSubview:line];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(evaluate.mas_bottom).offset(0);
                make.centerX.equalTo(evaluate);
                make.height.offset(130).priorityHigh();
                make.width.offset(0.5);
            }];
            
            UILabel *number=[[UILabel alloc]init];
            [self.headView addSubview:number];
            number.tag=1024+i;
            number.text=@"0";
            number.textAlignment=NSTextAlignmentCenter;
            number.textColor=[UIColor whiteColor];
            number.font=[UIFont systemFontOfSize:13];
            [number mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(line.mas_bottom).offset(0);
                make.left.offset(i*(SCREEN_WIDTH/3));
                make.height.offset(44);
                make.width.offset(SCREEN_WIDTH/3);
                make.bottom.offset(0);
            }];
        }
        
    
        self.accumulative=[[UILabel alloc]init];
        self.backgroundColor=[UIColor whiteColor];
        [self addSubview:self.accumulative];
        self.accumulative.text=[NSString stringWithFormat:@"%@:",Localized(@"累计")];
        self.accumulative.font=[UIFont systemFontOfSize:13];
        [self.accumulative mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(10);
            make.top.equalTo(self.headView.mas_bottom).offset(10);
        }];
        
        self.number=[[UILabel alloc]init];
        [self addSubview:self.number];
        self.number.text=@"0条";
        self.number.textColor=[UIColor colorWithHexString:@"#adadad"];
        self.number.font=[UIFont systemFontOfSize:13];
        [self.number mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.offset(-10);
            make.top.equalTo(self.headView.mas_bottom).offset(10);
        }];
        
        self.proportion =[[UILabel alloc]init];
        [self addSubview:self.proportion];
        self.proportion.text=@"0％";
        self.proportion.textColor=[UIColor orangeColor];
        self.proportion.font=[UIFont systemFontOfSize:24];
        [self.proportion mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.accumulative.mas_bottom).offset(17);
            make.centerX.offset(0);
        }];
        
        self.favorable=[[UILabel alloc]init];
        [self addSubview:self.favorable];
        self.favorable.text=Localized(@"好评率");
        self.favorable.textColor=[UIColor colorWithHexString:@"#adadad"];
        self.favorable.font=[UIFont systemFontOfSize:13];
        [self.favorable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.proportion.mas_bottom).offset(6);
            make.centerX.offset(0);
        }];
    }
    return self;
}
-(void)setModel:(CreditModel *)model{
    _model=model;
    self.number.text=[NSString stringWithFormat:@"%@%@",model.good.total,Localized(@"条")];
    
    NSInteger total=[model.good.total intValue]+[model.bad.total intValue]+[model.normal.total intValue];
    for (int i=0; i<3; i++) {
        UILabel *number=[self viewWithTag:1024+i];
        UIView *view=[self viewWithTag:2048+i];
        UIView *white=[[UIView alloc]init];
        white.backgroundColor=[UIColor whiteColor];
        [self.headView addSubview:white];
        [white mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(view);
            make.bottom.equalTo(view);
            make.height.offset(130*0.01);
            make.width.offset(20);
        }];
        [white.superview layoutIfNeeded];
        
        if (i==0) {
            
            number.text=model.good.total;
            double proportion=[model.good.total intValue]/total;
            self.proportion.text=[NSString stringWithFormat:@"%.0f%%",proportion*100];
            proportion==0 ? proportion=0.01 : proportion;
            [UIView animateWithDuration:0.5 animations:^{
                [white mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(view);
                    make.bottom.equalTo(view);
                    make.height.offset(130*proportion);
                    make.width.offset(20);
                }];
                [white.superview layoutIfNeeded];
            }];
            
        }
        if (i==1) {
            number.text=model.normal.total;
            double proportion=[model.normal.total intValue]/total;
            proportion==0 ? proportion=0.01 : proportion;
            [UIView animateWithDuration:0.5 animations:^{
                [white mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(view);
                    make.bottom.equalTo(view);
                    make.height.offset(130*proportion);
                    make.width.offset(20);
                }];
                [white.superview layoutIfNeeded];
            }];
        }
        if (i==2) {
            number.text=model.bad.total;
            double proportion=[model.bad.total intValue]/total;
            proportion==0 ? proportion=0.01 : proportion;
            [UIView animateWithDuration:0.5 animations:^{
                [white mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(view);
                    make.bottom.equalTo(view);
                    make.height.offset(130*proportion);
                    make.width.offset(20);
                }];
                [white.superview layoutIfNeeded];
            }];
        }
    }
}
-(void)buttonClick:(UIButton*)button
{
    //获取点击的是第几个button
    NSInteger index = button.tag - Button_Tag;
    [self setSelectAtIndex:index];
}
-(void)setSelectAtIndex:(NSInteger)index
{
    //1、先调整自身的视图显示
    for (int i = 0; i < self.items.count; i++) {
        UIButton * bt = [self viewWithTag:i+Button_Tag];
        
        if (bt.tag-Button_Tag == index) {
            bt.selected = YES;
            
        }else{
            bt.selected = NO;
        }
    }
    
}
@end
